package su2010.puz.extractors;

import java.awt.image.BufferedImage;

import su2010.puz.Extractor;
import su2010.puz.Filter;
import su2010.puz.ImageHelper;
import su2010.puz.util.ImageCls;
import su2010.puz.util.PixelExtractor;

public class DifferencePixelExtractor implements Extractor {

	private BufferedImage image;
	private int numOutput = 1;
	private Filter[] firstFilters;
	private Filter[] secondFilters;
	private String name = "";
	
	public DifferencePixelExtractor(String name) {
		this.name = name;
	}
	
	public void setFirstFilters(Filter ... firstFilters) {
		this.firstFilters = firstFilters;
	}
	
	public void setSecondFilters(Filter ... secondFilters) {
		this.secondFilters = secondFilters;
	}
	
	@Override
	public float[] extract() {
		BufferedImage firstImage = ImageHelper.getBufferedImage(image);
		BufferedImage secondImage = ImageHelper.getBufferedImage(image);
		
		if(firstFilters!=null)
			for(Filter filter : firstFilters)
				firstImage = filter.apply(firstImage);
		
		if(secondFilters!=null)
			for(Filter filter : secondFilters)
				secondImage = filter.apply(secondImage);
		
		BufferedImage diff = difference(firstImage, secondImage);
		
		PixelIntervalExtractor extractor = new PixelIntervalExtractor();
		extractor.setColorMode(ImageHelper.LUMINANCE);
		extractor.setInterval(1, 255);
		extractor.setImage(diff);

		return extractor.extract();
		
	}

	public BufferedImage difference(BufferedImage first, BufferedImage second){
		int width = first.getWidth();
		int height = first.getHeight();
		BufferedImage diff = new BufferedImage(width,height,image.getType());
		int[] firstPixels = new int[width*height];
		int[] secondPixels = new int[width*height];
		int[] difference = new int[width*height];
		PixelExtractor.getRGB(first, 0, 0, width, height, firstPixels);
		PixelExtractor.getRGB(second, 0, 0, width, height, secondPixels);
		for(int i = 0;i<width*height;i++){
			int d = firstPixels[i] - secondPixels[i];
			if(d < 0)
				d = 0;
			difference[i] = d; 
		}
		PixelExtractor.setRGB(diff, 0, 0, width, height, difference);
		return diff;
	}
	
	public static BufferedImage diff(BufferedImage first, BufferedImage second){
		int width = first.getWidth();
		int height = first.getHeight();
		BufferedImage diff = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
		int[] firstPixels = new int[width*height];
		int[] secondPixels = new int[width*height];
		int[] difference = new int[width*height];
		PixelExtractor.getRGB(first, 0, 0, width, height, firstPixels);
		PixelExtractor.getRGB(second, 0, 0, width, height, secondPixels);
		for(int i = 0;i<width*height;i++){
			int d = firstPixels[i] - secondPixels[i];
			if(d < 0)
				d = 0;
			difference[i] = d; 
		}
		PixelExtractor.setRGB(diff, 0, 0, width, height, difference);
		return diff;
	}
	
	@Override
	public int getNumOutput() {
		return numOutput;
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public void setImage(ImageCls image) {
		this.image = image.getImage();
	}

}
